package sections;

import static objects.AdapletConstants.sNewLine;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedHashMap;
import java.util.Map;

import objects.IObject;
import objects.ReferenceType;

public class ReferenceTypeSection implements ISection
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2786052044680499845L;
	private Map<String, ReferenceType> fReferenceTypeList;

	public ReferenceTypeSection()
	{
		fReferenceTypeList = new LinkedHashMap<String, ReferenceType>();
	}

	public ReferenceType getReferenceType(String typeName)
	{
		return fReferenceTypeList.get(typeName);
	}

	public ReferenceType createNewReferenceType(String typeName)
	{
		ReferenceType refType = new ReferenceType();

		refType.setTypeName(typeName);

		fReferenceTypeList.put(typeName, refType);

		return refType;
	}

	public String buildReferenceTypeSectionDefinition()
	{
		StringBuffer refTypeSecDef = new StringBuffer(fReferenceTypeList.size() * 200);

		refTypeSecDef.append(sNewLine);

		for (String typeName : fReferenceTypeList.keySet())
		{
			ReferenceType refType = fReferenceTypeList.get(typeName);

			refTypeSecDef.append(refType.toString());
			refTypeSecDef.append(sNewLine);
		}

		return refTypeSecDef.toString();
	}

	@Override
	public String toString()
	{
		return buildReferenceTypeSectionDefinition();
	}

	@Override
	public int getCount()
	{
		return fReferenceTypeList.size();
	}

	@Override
	public Section getSectionType()
	{
		return Section.REF_TYPE;
	}

	@Override
	public IObject[] getObjects()
	{
		return fReferenceTypeList.values().toArray(new ReferenceType[0]);
	}
	
	private void readObject(ObjectInputStream iStream) throws IOException, ClassNotFoundException
	{
		iStream.defaultReadObject();
		validateState();
	}
	
	private void validateState()
	{
		// TODO Auto-generated method stub
		
	}

	private void writeObject(ObjectOutputStream oStream) throws IOException
	{
		oStream.defaultWriteObject();
	}
}
